// pages/user/add-img/index.js
import api from '../../../../api/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    basePath: wx.$basePath,
    show: true,//显示选择图片的按钮
    photoList: [],
    maxPhoto: 9,//最大上传10张图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    api.user.getPhotos().then(lst=>{
      if(!wx.$isNull(lst)){
        this.setData({
          photoList: lst
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },




  /**
   * 选择上传方式
   * @param {*} e 
   */
  chooseImg(e) {
    if (this.NextTap) {
      return;
    }
    this.NextTap = true;
    setTimeout(() => {
      this.NextTap = false;
    }, 2000)//1.5秒之后可以再次点击，防止用户重复点击
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: (res) => {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            this.chooseWxImage('album')//相册
          } else if (res.tapIndex == 1) {
            this.chooseWxImage('camera')//拍照
          }
        }
      }
    })
  },
  /**
   * 上传照片
   * @param {*} type 
   */
  chooseWxImage: function (type) {
    let that = this
    let { photoList } = this.data
    if (photoList.length > this.data.maxPhoto) {
      wx.showToast({
        title: '最多上传'+ this.data.maxPhoto +'张',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: [type],
      sizeType: ['compressed'],
      camera: 'front',
      success: (res) => {
        const tempFilePath = res.tempFiles[0].tempFilePath
        wx.compressImage({
          src: tempFilePath,
          compressedHeight: 960,
          success: function (rs) {
            wx.compressImage({
              src: tempFilePath,
              compressedHeight: 144,
              success: function (mrs) {

                api.common.uploadPreview(mrs.tempFilePath).then(r=>{
                  api.common.uploadImage(rs.tempFilePath).then(rs=>{
                    const data ={
                      preview: r,
                      path: rs
                    }
                    api.user.setPhoto(data).then(res=>{
                      console.log(res)
                      if(res.code == 0){
                        that.onReady()
                      }else{
                        api.common.delFile(data.preview)
                        api.common.delFile(data.path)
                      }
                    })
                  })
                })

                console.log(mrs.tempFilePath)
              }
            })
            console.log(rs.tempFilePath)
          }
        })
      }

    })

    
  },
  /*
    * 图片预览
    * @param e
    */
  previewImg(e) {
    let currentUrl = e.currentTarget.dataset.src;
    let urls = []
    for(const i in this.data.photoList){
      urls.push(this.data.basePath + '/' + this.data.photoList[i].path)
    }
    wx.previewImage({
      current: this.data.basePath + '/' + currentUrl.path, // 当前显示图片的http链接
      urls: urls// 需要预览的图片http链接列表
    })
  },
  /**
   * 删除上传的图片
   * @param e
   */
  delImg(e) {
    wx.$confirm({
      content: '确认删除?',
      success: res=>{
        if (res.confirm){

          let { index } = e.currentTarget.dataset, { photoList } = this.data
          photoList.splice(index, 1)
          this.setData({
            photoList: photoList
          })
          const photo = e.currentTarget.dataset.src
          console.log(photo)
          api.user.delPhoto(photo.id)
        }
        
      }
    })

    

  }
})